package com.hn658.base.message.notisender.business.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.hn658.base.message.notisender.business.INotisenderManager;
import com.hn658.base.message.notisender.message.NotificationMessage;
import com.hn658.base.message.notisender.sender.IMessageSender;
import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.logging.enums.LogCategory;

/**
 * 
 * 通知消息发送管理器
 * @author ztjie
 * @date 2015-10-21 下午7:41:36
 * @since
 * @version
 */
@Component
public class NotisenderManagerImpl extends AbstractBusinessObject implements InitializingBean, ApplicationContextAware, INotisenderManager {
	
	private ApplicationContext context;
	
	/**
	 * 消息发送器Map
	 */
    private Map<Integer, IMessageSender<NotificationMessage>> messageSenders;

    @Override
    public void sendNotification(NotificationMessage message){
        logger.debug(LogCategory.SYSTEM,"notification Service is prepareing to send message:{}", message);
        // 获取消息对应的Handlers
        IMessageSender<NotificationMessage> messageSender = messageSenders.get(message.getType().getValue());

        logger.debug(LogCategory.SYSTEM,"为消息{} 找到处理器{}", new Object[]{message, messageSender});

        // 依次处理Message
        messageSender.send(message);
    }

	
	@Override
    public void sendNotification(List<NotificationMessage> messages){
		for(NotificationMessage message : messages){
			this.sendNotification(message);		
		}
    }
    
	@Override
	public void afterPropertiesSet() throws Exception {
		initMessageSender();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initMessageSender() {
		Map<String, IMessageSender> beans = context
				.getBeansOfType(IMessageSender.class);
		if (beans != null && beans.size() > 0) {
			logger.debug(LogCategory.ORDER,
					String.format("共找到%s个MessageSender相关的处理类", beans.size()));
			for (Map.Entry<String, IMessageSender> entry : beans
					.entrySet()) {
				IMessageSender<NotificationMessage> sender = entry.getValue();
				if (sender.getTarget() == null) {
					logger.error(LogCategory.ORDER, String.format("%s实现类方法getTarget方法不能返回空，必须返回有效值",sender.getClass()));
				} else {
					messageSenders.put(Integer.valueOf(sender.getTarget().getValue()), sender);
				}
			}
		} else {
			logger.debug(LogCategory.ORDER, String.format("没有找到相关的订单状态变更处理接口(MessageSender)实现类"));
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.context = applicationContext;
		messageSenders = new HashMap<Integer, IMessageSender<NotificationMessage>>();
	}
}
